﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace LitTools
{
    /// <summary>
    /// 索引过滤器
    /// < = > 
    /// </summary>
    public class IndexFilter : FilterBase
    {
        public enum CompareType
        {
            Less,
            Equal,
            Great,
        }

        [Header("比较方式")]
        public CompareType compareType;

        [Header("索引")]
        public int index;

        public override List<RaycastResult> FiltResults(List<RaycastResult> result)
        {
            if (index < 0)
            {
                Debug.LogError("index can't less than zero");
                return result;
            }
            if (result == null)
            {
                return result;
            }
            return compareType switch
            {
                CompareType.Less => FiltLessThanIndex(result),
                CompareType.Great => FiltGreatThanIndex(result),
                CompareType.Equal => FiltEqualIndex(result),
                _ => result,
            };
        }

        /// <summary>
        /// 小于当前索引
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private List<RaycastResult> FiltLessThanIndex(List<RaycastResult> result)
        {
            if (index > result.Count - 1)
            {
                return result;
            }
            for (int i = index; i < result.Count; i++)
            {
                result.RemoveAt(i);
                i--;
            }
            return result;
        }

        /// <summary>
        /// 等于当前索引
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private List<RaycastResult> FiltEqualIndex(List<RaycastResult> result)
        {
            if (index > result.Count - 1)
            {
                return null;
            }
            else if (index <= 0)
            {

            }
            return result;
        }

        /// <summary>
        /// 大于当前索引
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private List<RaycastResult> FiltGreatThanIndex(List<RaycastResult> result)
        {
            if (index >= result.Count - 1)
            {
                return null;
            }
            for (int i = index; i > 0 ; i--)
            {
                result.RemoveAt(i);
            }
            return result;
        }
    }
}